package com.software.zhemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_login;
    private Button btn_register;
    private EditText edt_name;
    private EditText edt_pwd;
    private LinearLayout layout_bg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //获取控件
        initViews();
        //第一种 匿名内部类
//        initEvents();
        //第二种 成员内部类
//        LoginListener listener = new LoginListener();
//        btn_login.setOnClickListener(listener);
//        btn_register.setOnClickListener(listener);
        //第三种
        extracted();
    }

    private void extracted() {
        btn_register.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    private void initEvents() {
        btn_login.setOnClickListener(v ->{
            //获取输入的内容
            String name = edt_name.getText().toString();
            String pwd = edt_pwd.getText().toString();
            //提示框
            Toast.makeText(this, name + ":" + pwd, Toast.LENGTH_SHORT).show();
        });
        btn_register.setOnClickListener(v ->{
            //提示框
            Toast.makeText(this, "欢迎注册我的app", Toast.LENGTH_SHORT).show();
        });

    }

    private void initViews() {
        btn_login = findViewById(R.id.btn_login);
        btn_register = findViewById(R.id.btn_register);
        edt_name = findViewById(R.id.edt_name);
        edt_pwd = findViewById(R.id.edt_pwd);
        layout_bg = findViewById(R.id.layout_bg);
        layout_bg.getBackground().setAlpha(100);

    }

    @Override
    public void onClick(View v) {
        //View v 参数，代表当前点击的控件
        int id = v.getId();

        if (id == R.id.btn_login){
            //获取输入的内容
            String name = edt_name.getText().toString();
            String pwd = edt_pwd.getText().toString();
            //提示框
            Toast.makeText(LoginActivity.this, name + ":" + pwd, Toast.LENGTH_SHORT).show();
        } else if (id == R.id.btn_register) {
            Toast.makeText(LoginActivity.this, "欢迎注册我的app", Toast.LENGTH_SHORT).show();
        }
    }

    class LoginListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            //View v 参数，代表当前点击的控件
            int id = v.getId();

            if (id == R.id.btn_login){
                //获取输入的内容
                String name = edt_name.getText().toString();
                String pwd = edt_pwd.getText().toString();
                //提示框
                Toast.makeText(LoginActivity.this, name + ":" + pwd, Toast.LENGTH_SHORT).show();
            } else if (id == R.id.btn_register) {
                Toast.makeText(LoginActivity.this, "欢迎注册我的app", Toast.LENGTH_SHORT).show();
            }
        }
    }
}